/* JavaLog - package tracker 
 * 
 * Copyright (C) 2009  Allyson Lima, Diego Pedro, Victor Freire
 * 
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software 
 * Foundation, either version 3 of the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
 * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this 
 * program.  If not, see <http://www.gnu.org/licenses/>.
 */

package br.edu.ufcg.ccc.javalog;

/**
 * Utilities for manipulating text strings.
 * 
 * @author Allyson Lima, Diego Pedro, Victor Freire
 * @version 08/11/09
 */
public class StringUtil {
	private static final String VALID_CHARACTERS;

	/**
	 * Static members initializer.
	 */
	static {
		StringBuilder validchars = new StringBuilder();
		for (char c = 'a'; c != 'z'; ++c)
			validchars.append(c);
		for (char c = 'A'; c != 'Z'; ++c)
			validchars.append(c);
		for (char c = '0'; c != '9'; ++c)
			validchars.append(c);
		validchars.append("'\"!?.-*");
		VALID_CHARACTERS = validchars.toString();
	}

	/**
	 * Searches the String for characters not listed as valid.
	 * 
	 * @param text
	 *            text to check
	 * @return the first invalid character found or null there are no invalid
	 *         characters.
	 */
	public static Character containsInvalidChar(String text) {
		for (char c : text.toCharArray())
			if (!VALID_CHARACTERS.contains(((Character) c).toString()))
				return c;
		return null;
	}

	/**
	 * Searches the String for characters not listed as valid.
	 * 
	 * @param text
	 *            text to check
	 * @param VALID_CHARACTERS
	 *            string with characters valid
	 * @return the first invalid character found or null there are no invalid
	 *         characters.
	 */
	public static Character containsInvalidChar(String text,
			String VALID_CHARACTERS) {
		for (char c : text.toCharArray())
			if (!VALID_CHARACTERS.contains(((Character) c).toString()))
				return c;
		return null;
	}

	/**
	 * fix white Space of a string
	 * 
	 * @param data
	 *            a data of string
	 * @return a data of string fixWhiteSpace
	 */
	public static String fixWhiteSpace(String data) {

		String dataWithOutWhiteSpace = "";
		data += " ";
		for (int i = 0; i < data.length() - 1; i++)
			if (data.substring(i, i + 1).equals(" ")
					&& !data.substring(i + 1, i + 2).equals(" "))
				dataWithOutWhiteSpace += data.substring(i, i + 1);
			else if (!data.substring(i, i + 1).equals(" "))
				dataWithOutWhiteSpace += data.substring(i, i + 1);

		if (data.substring(0, 1).equals(" "))
			dataWithOutWhiteSpace = dataWithOutWhiteSpace.substring(1,
					dataWithOutWhiteSpace.length());

		return dataWithOutWhiteSpace;
	}
}
